<template>
  <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" :class="className" />
  <svg v-else-if="!isElementIcon" class="svg-icon" :class="className" aria-hidden="true">
    <use :xlink:href="iconName" />
  </svg>
  <i v-else :class="icon" class="icon" :size="size" :color="color"></i>
</template>

<script setup lang="ts" >
import { isExternal as external } from '@/utils/validate'
import { computed } from 'vue'
const props = defineProps({
  // icon 图标
  icon: {
    type: String,
    required: true,
  },
  // 图标类名
  className: {
    type: String,
    default: '',
  },
  size: {
    default: () => '20px',
  },
  color: {
    default: () => '#fff',
  }
})

/**
 * 判断是否为外部图标
 */
const isExternal = computed(() => external(props.icon))

/**
 * 判断是否是ElementUI图标
 *
 */
const isElementIcon = computed(() => props.icon?.startsWith('el-icon'))


/**
 * 外部图标样式
 */
const styleExternalIcon = computed(() => ({
  mask: `url(${props.icon}) no-repeat 50% 50%`,
  '-webkit-mask': `url(${props.icon}) no-repeat 50% 50%`,
}))
/**
 * 项目内图标
 */
const iconName = computed(() => `#icon-${props.icon}`)
</script>

<style scoped>
.svg-icon {
  width: 1.5em;
  height: 1.5em;
  fill: currentColor;
  vertical-align: middle;
  margin-right: 5px;
  text-align: center;
  overflow: hidden;
  color: "#f8ebeb";
  font-size: 1em;
}

.svg-external-icon {
  background-color: currentColor;
  mask-size: cover !important;
  display: inline-block;
}

.icon {
  font-size: 20px;
}
</style>
